<template>
  <div v-if="visible" class="modal-overlay" @click.self="handleClose">
    <div class="modal-content">
      <div class="modal-header">
        <h2 class="modal-title">🎉 游戏结束 🎉</h2>
        <button class="close-btn" @click="handleClose">×</button>
      </div>

      <div class="modal-body">
        <div class="ranks-section">
          <h3 class="section-title">最终名次</h3>
          <div class="ranks-list">
            <div
              v-for="(playerId, index) in ranksWithNames"
              :key="playerId"
              class="rank-item"
              :class="{ 'rank-item--ai': isAIPlayer(playerId) }"
            >
              <div class="rank-number">{{ getRankLabel(index + 1) }}</div>
              <div class="player-info">
                <span class="player-name">{{ getPlayerName(playerId) }}</span>
                <span v-if="isAIPlayer(playerId)" class="ai-badge">AI</span>
              </div>
            </div>
          </div>

          <!-- 如果还有未完成的玩家，显示提示 -->
          <div v-if="hasUnfinishedPlayers" class="unfinished-hint">
            <p>注：部分玩家尚未出完牌</p>
          </div>
        </div>

        <div class="actions">
          <button class="btn btn-primary" @click="handleNewGame">
            开始新游戏
          </button>
          <button class="btn btn-secondary" @click="handleClose">关闭</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { Player, PlayerType } from '../types/game'

interface Props {
  visible: boolean
  ranks: number[] // 玩家名次数组
  players: Player[] // 所有玩家
}

interface Emits {
  (e: 'close'): void
  (e: 'newGame'): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

// 计算属性
const ranksWithNames = computed(() => {
  return props.ranks
})

const hasUnfinishedPlayers = computed(() => {
  return props.ranks.length < props.players.length
})

// 方法
const getPlayerName = (playerId: number): string => {
  const player = props.players.find((p) => p.id === playerId)
  return player ? player.name : `玩家${playerId}`
}

const isAIPlayer = (playerId: number): boolean => {
  const player = props.players.find((p) => p.id === playerId)
  return player?.type === PlayerType.AI
}

const getRankLabel = (rank: number): string => {
  const labels = ['🥇', '🥈', '🥉', '4️⃣']
  return labels[rank - 1] || `${rank}`
}

const handleClose = () => {
  emit('close')
}

const handleNewGame = () => {
  emit('newGame')
}
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.modal-content {
  background: linear-gradient(135deg, #1a472a 0%, #0d2818 100%);
  border-radius: 16px;
  border: 3px solid rgba(253, 126, 20, 0.5);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.5);
  max-width: 500px;
  width: 90%;
  max-height: 80vh;
  overflow: hidden;
  animation: slideIn 0.3s ease;
}

@keyframes slideIn {
  from {
    transform: translateY(-50px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 2px solid rgba(253, 126, 20, 0.3);
  background: rgba(0, 0, 0, 0.3);
}

.modal-title {
  margin: 0;
  color: #ffc107;
  font-size: 24px;
  font-weight: bold;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
}

.close-btn {
  background: none;
  border: none;
  color: white;
  font-size: 32px;
  cursor: pointer;
  padding: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.2s;
}

.close-btn:hover {
  color: #fd7e14;
}

.modal-body {
  padding: 24px;
  overflow-y: auto;
  max-height: calc(80vh - 80px);
}

.ranks-section {
  margin-bottom: 24px;
}

.section-title {
  color: #ffc107;
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 16px;
  text-align: center;
}

.ranks-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.rank-item {
  display: flex;
  align-items: center;
  gap: 16px;
  background: rgba(0, 0, 0, 0.3);
  border: 2px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  padding: 12px 16px;
  transition: all 0.3s ease;
}

.rank-item:hover {
  border-color: rgba(253, 126, 20, 0.5);
  transform: translateX(4px);
}

.rank-item--ai {
  border-color: rgba(111, 66, 193, 0.5);
  background: rgba(111, 66, 193, 0.1);
}

.rank-number {
  font-size: 32px;
  min-width: 40px;
  text-align: center;
}

.player-info {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
}

.player-name {
  color: white;
  font-size: 16px;
  font-weight: bold;
}

.ai-badge {
  background: linear-gradient(135deg, #6f42c1, #9d4edd);
  color: white;
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: bold;
}

.unfinished-hint {
  margin-top: 16px;
  padding: 12px;
  background: rgba(255, 193, 7, 0.2);
  border: 1px solid rgba(255, 193, 7, 0.5);
  border-radius: 8px;
  text-align: center;
}

.unfinished-hint p {
  margin: 0;
  color: #ffc107;
  font-size: 14px;
}

.actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.btn {
  padding: 10px 24px;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-primary {
  background: linear-gradient(135deg, #fd7e14, #ffc107);
  color: white;
  box-shadow: 0 4px 12px rgba(253, 126, 20, 0.4);
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(253, 126, 20, 0.6);
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.1);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.btn-secondary:hover {
  background: rgba(255, 255, 255, 0.2);
}
</style>
